package aar.test.tessocr.base;


import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.DisplayMetrics;



import java.util.Locale;

import aar.test.tessocr.utils.ConstantPara;
import aar.test.tessocr.utils.LogUtils;
import aar.test.tessocr.utils.PrefUtils;

public class BaseActivity extends AppCompatActivity {
    AppManager appManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appManager = AppManager.getAppManager();
        // 把当前activity加入栈中
        appManager.addActivity(this);
        appManager.getSize("销毁前堆栈activity数目");

        changeLanguage();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // activity关闭，从栈中移除
        appManager.finishActivity(this);
        appManager.getSize("销毁后堆栈activity数目");


    }


    @Override
    protected void onResume() {
        super.onResume();
        changeLanguage();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        changeLanguage();
    }

    private void changeLanguage() {

        String language = PrefUtils.getString(this, ConstantPara.CHANGE_LANGUAGE_PREF_KEY, "zh");
        LogUtils.e("language","language=="+language);
        // 本地语言设置
        Locale myLocale = new Locale(language);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);

    }






}

